package com.amazon.web.util;

import com.amazon.bo.Ean;
import com.amazon.bo.Product;
import com.amazon.bo.Shop;
import com.amazon.bo.ShopClass;
import com.amazon.dao.EanDao;
import com.thoughtworks.xstream.XStream;
import com.translate.TransApi;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.StringWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by anhui on 2018/4/16.
 */
public class XMLUtil {


    private static String PRODUCT_TEMPLATE="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
            "<AmazonEnvelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"amznenvelope.xsd\">\n"+
                "<Header>\n"+
                    "<DocumentVersion>1.01</DocumentVersion>\n"+
                    "<MerchantIdentifier>{sid}</MerchantIdentifier>\n"+
                "</Header>\n"+
                "<MessageType>Product</MessageType>\n"+
                "<PurgeAndReplace>false</PurgeAndReplace>\n"+
                "{products}\n"+
            "</AmazonEnvelope>";


    private static final String APP_ID = "20180715000185622";
    private static final String SECURITY_KEY = "RtGMlsmDQUDfmQ6Ddi5L";

   private static TransApi api = new TransApi(APP_ID, SECURITY_KEY);


    private static String PRODUCT_INVENTORY_TEMPLATE="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                                                        "<AmazonEnvelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"amznenvelope.xsd\">  \n" +
                                                            "<Header> \n" +
                                                                "<DocumentVersion>1.01</DocumentVersion>  \n" +
                                                                "<MerchantIdentifier>{sid}</MerchantIdentifier> \n" +
                                                            "</Header>  \n" +
                                                            "<MessageType>Inventory</MessageType>  \n" +
                                                            "{inventory}\n"+
                                                        "</AmazonEnvelope>\n";


    /***
     *
     * {SKU},{StartDate} 2018-10-01T00:00:00Z,{EndDate},{price}
     */
    private static String PRODUCT_PRICE_TEMPLATE="<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
                                                    "<AmazonEnvelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"amznenvelope.xsd\">\n" +
                                                        "<Header>\n" +
                                                            "<DocumentVersion>1.01</DocumentVersion>\n" +
                                                            "<MerchantIdentifier>{sid}</MerchantIdentifier>\n" +
                                                        "</Header>\n" +
                                                        "<MessageType>Price</MessageType>\n" +
                                                        "{prices}"+
                                                    "</AmazonEnvelope>";




    private static String PRODUCT_IMAGE_TEMPLATE="<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
                                                "<AmazonEnvelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"amznenvelope.xsd\">\n" +
                                                    "<Header>\n" +
                                                        "<DocumentVersion>1.01</DocumentVersion>\n" +
                                                        "<MerchantIdentifier>{sid}</MerchantIdentifier>\n" +
                                                    "</Header>\n" +
                                                    "<MessageType>ProductImage</MessageType>\n" +
                                                    "{imgs}"+
                                                "</AmazonEnvelope>";



    private static String PRODUCT_VAR_TEMPLATE="<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
            "<AmazonEnvelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"amznenvelope.xsd\">\n" +
            "<Header>\n" +
            "<DocumentVersion>1.01</DocumentVersion>\n" +
            "<MerchantIdentifier>{sid}</MerchantIdentifier>\n" +
            "</Header>\n" +
            "<MessageType>Relationship</MessageType>\n" +
            "{vars}"+
            "</AmazonEnvelope>";


    /**
     * JSON(数组)字符串转换成XML字符串
     * （必须引入 xom-1.1.jar）
     * @param jsonString
     * @return
     */
    public static String json2xml(String jsonString) {
        XMLSerializer xmlSerializer = new XMLSerializer();
        return xmlSerializer.write(JSONSerializer.toJSON(jsonString));
    }
    /**
     * xml 转 json
     * （必须引入 xom-1.1.jar）
     * @param xmlString xml字符串
     * @return
     */
    public static String xml2json(String xmlString) {
        XMLSerializer xmlSerializer = new XMLSerializer();
        JSON json = xmlSerializer.read(xmlString);
        return json.toString(1);
    }




    public static String toXML(List<Product> productList, Shop shop, ShopClass shopClass,EanDao eanDao){

         String productItem ="<Message>\n"+
                                        "<MessageID>{index}</MessageID>\n"+
                                        "<OperationType>Update</OperationType>\n"+
                                        "<Product>\n"+
                                        "<SKU>{SKU}</SKU>\n"+
                                        "<StandardProductID>" +
                                        "<Type>EAN</Type>" +
                                        "<Value>{StandardProductID}</Value>" +
                                        "</StandardProductID>\n"+
                                        "<LaunchDate>{LaunchDate}</LaunchDate>\n"+
                                        "<Condition><ConditionType>{Condition}</ConditionType></Condition>\n"+
                                        "<DescriptionData>\n"+
                                            "<Title>{Title}</Title>\n"+
                                            "<Brand>{Brand}</Brand>\n"+
                                            "<Description>{Description}</Description>\n"+
                                            "{BulletPoint}\n"+
                                            "<Manufacturer>{Manufacturer}</Manufacturer>\n"+
                                            "<MfrPartNumber>ABCD</MfrPartNumber>\n"+
                                            "{SearchTerms}\n"+
                                            "<IsGiftWrapAvailable>false</IsGiftWrapAvailable>\n"+
                                            "<IsGiftMessageAvailable>false</IsGiftMessageAvailable>\n"+
                                             "<RecommendedBrowseNode>{ItemType1}</RecommendedBrowseNode>\n"+
                                             "<RecommendedBrowseNode>{ItemType2}</RecommendedBrowseNode>\n"+
//                                             "<MerchantShippingGroupName>Heavy Bulky Products</MerchantShippingGroupName>\n"+
//                                             "<FEDAS_ID>123456</FEDAS_ID>\n"+

                                        "</DescriptionData>\n"+
                                        "<ProductData>\n"+
                                        "<Home>\n"+
                                        "<ProductType>\n"+
                                        "<Home>\n"+
                        //                                "<BatteryCellType>manganese</BatteryCellType>\n"+
                        //                                "<BatteryChargeCycles></BatteryChargeCycles>"+ //电池充电周期
                        //                                "<BatteryPower></BatteryPower>"+//度量单位
                        //                                "<FinishType></FinishType>"+//最终
                        //                                "<InternetApplications></InternetApplications>"+
                        //                                "<MemorySlotsAvailable></MemorySlotsAvailable>"+
                        //                                "<PowerPlugType></PowerPlugType>"+
                        //                                "<PowerSource></PowerSource>"+
//                                                        "<ScreenSize>{ScreenSize}</ScreenSize>"+
                        //                                "<SoftwareIncluded></SoftwareIncluded>"+
                        //                                "<WirelessTechnology></WirelessTechnology>"+
                                        "</Home>\n"+
                                        "</ProductType>\n"+
                                        "</Home>\n"+
                                        "</ProductData>\n"+
                                        "</Product>\n"+
                                        "</Message>\n";





                String html ="";


                 List<Product> skuList= new ArrayList<>();

                 for (Product p :productList){

                     p.setSearchTerms("Cap\nHat");
                     if(p.getProductClass().equals("")){
                         p.setProductClass("1730954031,1730957031");
                     }



                     //英国
                     if(shop.getVisitcode().equals("A1F83G8C2ARO7P")){


                     }else if(shop.getVisitcode().equals("A13V1IB3VIYZZH")){
                        //法国
                         // region 内容
                         if(p.getDescription()==null){
                             continue;
                         }
                         if(p.getFrDescription()==null) {
                             String desc_fr = "";
                             String[] desc = p.getDescription().split("\n");
                             for(String des :desc){
                                 Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "fra"));
                                 List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                                 String frDes = list.get(0).get("dst").toString();

                                 desc_fr+=frDes+"\n";
                             }
                             p.setFrDescription(desc_fr);
                         }
                         // endregion 内容

                         //region bulletPoints

                         if(p.getBulletPoint()==null){
                             continue;
                         }
                         if(p.getFrBulletPoint()==null){

                             String desc_Point = "";
                             String[] points = p.getBulletPoint().split("\n");

                             for(String des :points){
                                 Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "fra"));
                                 List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                                 String frDes = list.get(0).get("dst").toString();

                                 desc_Point+=frDes+"\n";
                             }
                             p.setFrBulletPoint(desc_Point);
                         }


                         //endregion

                         //region title
                         if(p.getTitle()==null){
                             continue;
                         }
                         if(p.getFrTitle()==null){

                             String desc_Point = "";
                             String[] points = p.getTitle().split("\n");

                             for(String des :points){
                                 Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "fra"));
                                 List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                                 String frDes = list.get(0).get("dst").toString();

                                 desc_Point+=frDes+"\n";
                             }
                             p.setFrTitle(desc_Point);
                         }
                         //endregion

                         //region search
                         if(p.getFrSearchTerms()==null){

                             String desc_Point = "";
                             String[] points = p.getSearchTerms().split("\n");

                             for(String des :points){
                                 Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "fra"));
                                 List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                                 String frDes = list.get(0).get("dst").toString();

                                 desc_Point+=frDes+"\n";
                             }
                             p.setFrSearchTerms(desc_Point);
                         }
                         //endregion

                         //region itemType


                         //endregion

                     }else if(shop.getVisitcode().equals("A1PA6795UKMFR9")){
                        //德国
                         // region 内容
                         if(p.getDescription()==null){
                             continue;
                         }
                         String desc_de = "";
                         String[] desc = p.getDescription().split("\n");
                         for(String des :desc){
                             Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "de"));
                             List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                             String deDes = list.get(0).get("dst").toString();

                             desc_de+=deDes+"\n";
                         }
                         p.setDeDescription(desc_de);
                         // endregion

                         //region bulletPoints
                         if(p.getBulletPoint()==null){
                             continue;
                         }
                         if(p.getDeBulletPoint()==null){

                             String desc_Point = "";
                             String[] points = p.getBulletPoint().split("\n");

                             for(String des :points){
                                 Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "de"));
                                 List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                                 String frDes = list.get(0).get("dst").toString();

                                 desc_Point+=frDes+"\n";
                             }
                             p.setDeBulletPoint(desc_Point);
                         }
                         //endregion

                         //region title
                         if(p.getTitle()==null){
                             continue;
                         }
                         if(p.getDeTitle()==null){

                             String desc_Point = "";
                             String[] points = p.getTitle().split("\n");

                             for(String des :points){
                                 Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "de"));
                                 List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                                 String frDes = list.get(0).get("dst").toString();

                                 desc_Point+=frDes+"\n";
                             }
                             p.setDeTitle(desc_Point);
                         }
                         //endregion

                         //region search
                         if(p.getItDescription()==null){

                             String desc_Point = "";
                             String[] points = p.getSearchTerms().split("\n");

                             for(String des :points){
                                 Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "de"));
                                 List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                                 String frDes = list.get(0).get("dst").toString();

                                 desc_Point+=frDes+"\n";
                             }
                             p.setDeSearchTerms(desc_Point);
                         }
                         //endregion


                     }else if(shop.getVisitcode().equals("APJ6JRA9NG5V4")){
                        //意大利
                         // region 内容
                         if(p.getDescription()==null){
                             continue;
                         }

                         String desc_it = "";
                         String[] desc = p.getDescription().split("\n");
                         for(String des :desc){
                             Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "it"));
                             List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                             String itDes = list.get(0).get("dst").toString();

                             desc_it+=itDes+"\n";
                         }
                         p.setItDescription(desc_it);
                         // endregion

                         //region bulletPoints
                         if(p.getBulletPoint()==null){
                             continue;
                         }
                         if(p.getItBulletPoint()==null){

                             String desc_Point = "";
                             String[] points = p.getBulletPoint().split("\n");

                             for(String des :points){
                                 Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "it"));
                                 List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                                 String frDes = list.get(0).get("dst").toString();

                                 desc_Point+=frDes+"\n";
                             }
                             p.setItBulletPoint(desc_Point);
                         }
                         //endregion

                         //region title
                         if(p.getTitle()==null){
                             continue;
                         }
                         if(p.getItTitle()==null){

                             String desc_Point = "";
                             String[] points = p.getTitle().split("\n");

                             for(String des :points){
                                 Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "it"));
                                 List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                                 String frDes = list.get(0).get("dst").toString();

                                 desc_Point+=frDes+"\n";
                             }
                             p.setItTitle(desc_Point);
                         }
                         //endregion

                         //region search
                         if(p.getItSearchTerms()==null){

                             String desc_Point = "";
                             String[] points = p.getSearchTerms().split("\n");

                             for(String des :points){
                                 Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "it"));
                                 List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                                 String frDes = list.get(0).get("dst").toString();

                                 desc_Point+=frDes+"\n";
                             }
                             p.setItSearchTerms(desc_Point);
                         }
                         //endregion


                     }else if(shop.getVisitcode().equals("A1RKKUPIHCS9HS")){

                         //西班牙
                         // region 内容
                         if(p.getDescription()==null){
                             continue;
                         }
                         String desc_spa = "";
                         String[] desc = p.getDescription().split("\n");
                         for(String des :desc){
                             Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "spa"));
                             List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                             String spaDes = list.get(0).get("dst").toString();

                             desc_spa+=spaDes+"\n";
                         }
                         p.setEsDescription(desc_spa);
                         // endregion

                         //region bulletPoints
                         if(p.getBulletPoint()==null){
                             continue;
                         }
                         if(p.getEsBulletPoint()==null){

                             String desc_Point = "";
                             String[] points = p.getBulletPoint().split("\n");

                             for(String des :points){
                                 Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "spa"));
                                 List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                                 String frDes = list.get(0).get("dst").toString();

                                 desc_Point+=frDes+"\n";
                             }
                             p.setEsBulletPoint(desc_Point);
                         }
                         //endregion

                         //region title
                         if(p.getTitle()==null){
                             continue;
                         }
                         if(p.getEsTitle()==null){

                             String desc_Point = "";
                             String[] points = p.getTitle().split("\n");

                             for(String des :points){
                                 Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "spa"));
                                 List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                                 String frDes = list.get(0).get("dst").toString();

                                 desc_Point+=frDes+"\n";
                             }
                             p.setEsTitle(desc_Point);
                         }
                         //endregion

                         //region search
                         if(p.getEsSearchTerms()==null){

                             String desc_Point = "";
                             String[] points = p.getSearchTerms().split("\n");

                             for(String des :points){
                                 Map<String,Object> result = Tools.parserJson2Map(api.getTransResult(des, "en", "spa"));
                                 List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("trans_result");
                                 String frDes = list.get(0).get("dst").toString();

                                 desc_Point+=frDes+"\n";
                             }
                             p.setEsSearchTerms(desc_Point);
                         }
                         //endregion

                     }

                 }



                 for(Product product :productList){
                    String[] skus =  product.getProps().split(";");
                     for(int i = 0;i<skus.length;i++){
                         Product skuProduct = new Product();
                         skuProduct.setSKU( shop.getId()+ "-"+product.getSKU()+"-"+ String.valueOf(i));
                         skuProduct.setConditions(product.getConditions());

                         skuProduct.setTitle(product.getTitle());
                         skuProduct.setFrTitle(product.getFrTitle());
                         skuProduct.setDeTitle(product.getDeTitle());
                         skuProduct.setItTitle(product.getItTitle());
                         skuProduct.setEsTitle(product.getEsTitle());

                         skuProduct.setDescription(product.getDescription());
                         skuProduct.setFrDescription(product.getFrDescription());
                         skuProduct.setDeDescription(product.getDeDescription());
                         skuProduct.setItDescription(product.getItDescription());
                         skuProduct.setEsDescription(product.getEsDescription());

                         skuProduct.setProductClass(product.getProductClass());

                         skuProduct.setBulletPoint(product.getBulletPoint());
                         skuProduct.setFrBulletPoint(product.getFrBulletPoint());
                         skuProduct.setDeBulletPoint(product.getDeBulletPoint());
                         skuProduct.setItBulletPoint(product.getItBulletPoint());
                         skuProduct.setEsBulletPoint(product.getEsBulletPoint());

                         skuProduct.setSearchTerms("Cap\nHat");
                         skuProduct.setFrSearchTerms(product.getFrSearchTerms());
                         skuProduct.setDeSearchTerms(product.getDeSearchTerms());
                         skuProduct.setItSearchTerms(product.getItSearchTerms());
                         skuProduct.setEsSearchTerms(product.getEsSearchTerms());

                         Ean ean = eanDao.selectLastEan();
                         skuProduct.setStandardProductID(ean.getEan());
                         ean.setEanStatus("1");
                         eanDao.update(ean);

                         skuList.add(skuProduct);
                     }
                 }
                 productList.addAll(skuList);

                 for(int i=0;i<productList.size();i++){

                     Product product = productList.get(i);

                     if(shop.getVisitcode().equals("A1F83G8C2ARO7P")){

                         String xml=productItem.replace("{SKU}",shop.getId()+ "-"+product.getSKU());
                         xml=xml.replace("{StandardProductID}",product.getStandardProductID());
                         xml=xml.replace("{LaunchDate}","2018-05-26T00:00:01");
                         xml=xml.replace("{Condition}",product.getConditions());
                         xml=xml.replace("{Title}",product.getTitle());
                         xml=xml.replace("{Brand}",shopClass.getBrand());
                         xml=xml.replace("{Description}",product.getDescription());
                         xml=xml.replace("{Manufacturer}",shopClass.getDesigner());

                         String[] keywords= product.getSearchTerms().split("\n");
                         String template ="";
                         for(String word :keywords) {
                             template += "<SearchTerms>" + word + "</SearchTerms>\n";
                         }

                         xml=xml.replace("{SearchTerms}",template);
                         xml=xml.replace("{index}",String.valueOf(i+1));
                         String[] itemTypes =product.getProductClass().split(",");
                         if(itemTypes.length>1){
                             xml=xml.replace("{ItemType1}",itemTypes[0]);
                             xml=xml.replace("{ItemType2}",itemTypes[1]);
                         }else {
                             xml = xml.replace("{ItemType1}", itemTypes[0]);
                             xml = xml.replace("{ItemType2}", "340322031");
                         }

                         String[] points = product.getBulletPoint().split("\n");
                         int max = points.length>5?5:points.length;
                         String bulletPoints="";
                         for(int j=0;j<max;j++){
                             bulletPoints+= "<BulletPoint>"+points[j]+"</BulletPoint>\n";
                         }
                         xml=xml.replace("{BulletPoint}",bulletPoints);


                         html+=xml;

                     }else if(shop.getVisitcode().equals("A13V1IB3VIYZZH")){
                         //法国
                         String xml=productItem.replace("{SKU}",shop.getId()+ "-"+product.getSKU());
                         xml=xml.replace("{StandardProductID}",product.getStandardProductID());
                         xml=xml.replace("{LaunchDate}","2018-05-26T00:00:01");
                         xml=xml.replace("{Condition}",product.getConditions());
                         xml=xml.replace("{Title}",product.getFrTitle());
                         xml=xml.replace("{Brand}",shopClass.getBrand());
                         xml=xml.replace("{Description}",product.getFrDescription());
                         xml=xml.replace("{Manufacturer}",shopClass.getDesigner());

                         String[] keywords= product.getFrSearchTerms().split("\n");
                         String template ="";
                         for(String word :keywords) {
                             template += "<SearchTerms>" + word + "</SearchTerms>\n";
                         }

                         xml=xml.replace("{SearchTerms}",template);
                         xml=xml.replace("{index}",String.valueOf(i+1));
                         String[] itemTypes =product.getProductClass().split(",");
                         if(itemTypes.length>1){

                             Map<String,Object> fraMap=eanDao.getIdMap(itemTypes[0]);
                             xml=xml.replace("{ItemType1}",fraMap.get("fr").toString());
                             fraMap=eanDao.getIdMap(itemTypes[1]);
                             xml=xml.replace("{ItemType2}",fraMap.get("fr").toString());
                         }else {

                             Map<String,Object> fraMap=eanDao.getIdMap(itemTypes[0]);
                             xml=xml.replace("{ItemType1}",fraMap.get("fr").toString());
                             fraMap=eanDao.getIdMap("340322031");
                             xml = xml.replace("{ItemType2}", fraMap.get("fr").toString());
                         }

                         String[] points = product.getFrBulletPoint().split("\n");
                         int max = points.length>5?5:points.length;
                         String bulletPoints="";
                         for(int j=0;j<max;j++){
                             bulletPoints+= "<BulletPoint>"+points[j]+"</BulletPoint>\n";
                         }
                         xml=xml.replace("{BulletPoint}",bulletPoints);


                         html+=xml;
                     }else if(shop.getVisitcode().equals("A1PA6795UKMFR9")){
                         //德国
                         String xml=productItem.replace("{SKU}",shop.getId()+ "-"+product.getSKU());
                         xml=xml.replace("{StandardProductID}",product.getStandardProductID());
                         xml=xml.replace("{LaunchDate}","2018-05-26T00:00:01");
                         xml=xml.replace("{Condition}",product.getConditions());
                         xml=xml.replace("{Title}",product.getDeTitle());
                         xml=xml.replace("{Brand}",shopClass.getBrand());
                         xml=xml.replace("{Description}",product.getDeDescription());
                         xml=xml.replace("{Manufacturer}",shopClass.getDesigner());

                         String[] keywords= product.getDeSearchTerms().split("\n");
                         String template ="";
                         for(String word :keywords) {
                             template += "<SearchTerms>" + word + "</SearchTerms>\n";
                         }

                         xml=xml.replace("{SearchTerms}",template);
                         xml=xml.replace("{index}",String.valueOf(i+1));
                         String[] itemTypes =product.getProductClass().split(",");
                         if(itemTypes.length>1){

                             Map<String,Object> fraMap=eanDao.getIdMap(itemTypes[0]);
                             xml=xml.replace("{ItemType1}",fraMap.get("de").toString());
                             fraMap=eanDao.getIdMap(itemTypes[1]);
                             xml=xml.replace("{ItemType2}",fraMap.get("de").toString());
                         }else {

                             Map<String,Object> fraMap=eanDao.getIdMap(itemTypes[0]);
                             xml=xml.replace("{ItemType1}",fraMap.get("de").toString());
                             fraMap=eanDao.getIdMap("340322031");
                             xml = xml.replace("{ItemType2}", fraMap.get("de").toString());
                         }

                         String[] points = product.getDeBulletPoint().split("\n");
                         int max = points.length>5?5:points.length;
                         String bulletPoints="";
                         for(int j=0;j<max;j++){
                             bulletPoints+= "<BulletPoint>"+points[j]+"</BulletPoint>\n";
                         }
                         xml=xml.replace("{BulletPoint}",bulletPoints);


                         html+=xml;
                     }else if(shop.getVisitcode().equals("APJ6JRA9NG5V4")){
                         //意大利
                         String xml=productItem.replace("{SKU}",shop.getId()+ "-"+product.getSKU());
                         xml=xml.replace("{StandardProductID}",product.getStandardProductID());
                         xml=xml.replace("{LaunchDate}","2018-05-26T00:00:01");
                         xml=xml.replace("{Condition}",product.getConditions());
                         xml=xml.replace("{Title}",product.getItTitle());
                         xml=xml.replace("{Brand}",shopClass.getBrand());
                         xml=xml.replace("{Description}",product.getItDescription());
                         xml=xml.replace("{Manufacturer}",shopClass.getDesigner());

                         String[] keywords= product.getItSearchTerms().split("\n");
                         String template ="";
                         for(String word :keywords) {
                             template += "<SearchTerms>" + word + "</SearchTerms>\n";
                         }

                         xml=xml.replace("{SearchTerms}",template);
                         xml=xml.replace("{index}",String.valueOf(i+1));
                         String[] itemTypes =product.getProductClass().split(",");
                         if(itemTypes.length>1){

                             Map<String,Object> fraMap=eanDao.getIdMap(itemTypes[0]);
                             xml=xml.replace("{ItemType1}",fraMap.get("it").toString());
                             fraMap=eanDao.getIdMap(itemTypes[1]);
                             xml=xml.replace("{ItemType2}",fraMap.get("it").toString());
                         }else {

                             Map<String,Object> fraMap=eanDao.getIdMap(itemTypes[0]);
                             xml=xml.replace("{ItemType1}",fraMap.get("it").toString());
                             fraMap=eanDao.getIdMap("340322031");
                             xml = xml.replace("{ItemType2}", fraMap.get("it").toString());
                         }

                         String[] points = product.getItBulletPoint().split("\n");
                         int max = points.length>5?5:points.length;
                         String bulletPoints="";
                         for(int j=0;j<max;j++){
                             bulletPoints+= "<BulletPoint>"+points[j]+"</BulletPoint>\n";
                         }
                         xml=xml.replace("{BulletPoint}",bulletPoints);


                         html+=xml;

                     }else if(shop.getVisitcode().equals("A1RKKUPIHCS9HS")){
                         //西班牙

                         String xml=productItem.replace("{SKU}",shop.getId()+ "-"+product.getSKU());
                         xml=xml.replace("{StandardProductID}",product.getStandardProductID());
                         xml=xml.replace("{LaunchDate}","2018-05-26T00:00:01");
                         xml=xml.replace("{Condition}",product.getConditions());
                         xml=xml.replace("{Title}",product.getEsTitle());
                         xml=xml.replace("{Brand}",shopClass.getBrand());
                         xml=xml.replace("{Description}",product.getEsDescription());
                         xml=xml.replace("{Manufacturer}",shopClass.getDesigner());

                         String[] keywords= product.getEsSearchTerms().split("\n");
                         String template ="";
                         for(String word :keywords) {
                             template += "<SearchTerms>" + word + "</SearchTerms>\n";
                         }

                         xml=xml.replace("{SearchTerms}",template);
                         xml=xml.replace("{index}",String.valueOf(i+1));
                         String[] itemTypes =product.getProductClass().split(",");
                         if(itemTypes.length>1){

                             Map<String,Object> fraMap=eanDao.getIdMap(itemTypes[0]);
                             xml=xml.replace("{ItemType1}",fraMap.get("es").toString());
                             fraMap=eanDao.getIdMap(itemTypes[1]);
                             xml=xml.replace("{ItemType2}",fraMap.get("es").toString());
                         }else {

                             Map<String,Object> fraMap=eanDao.getIdMap(itemTypes[0]);
                             xml=xml.replace("{ItemType1}",fraMap.get("es").toString());
                             fraMap=eanDao.getIdMap("340322031");
                             xml = xml.replace("{ItemType2}", fraMap.get("es").toString());
                         }

                         String[] points = product.getEsBulletPoint().split("\n");
                         int max = points.length>5?5:points.length;
                         String bulletPoints="";
                         for(int j=0;j<max;j++){
                             bulletPoints+= "<BulletPoint>"+points[j]+"</BulletPoint>\n";
                         }
                         xml=xml.replace("{BulletPoint}",bulletPoints);

                         html+=xml;
                     }


                 }
                 String xmlProduct = PRODUCT_TEMPLATE.replace("{sid}",shop.getShopcode());
                    xmlProduct= xmlProduct.replace("{products}",html);
                    return xmlProduct;
    }

    public static String toInventoryXML(List<Product> productList,Shop shop ) {



        String inventoryItem="<Message> \n" +
                "<MessageID>{index}</MessageID>  \n" +
                "<OperationType>Update</OperationType>  \n" +
                "<Inventory> \n" +
                "<SKU>{SKU}</SKU>  \n" +
                "<Quantity>99</Quantity>  \n" +
                "<FulfillmentLatency>1</FulfillmentLatency> \n" +
                "</Inventory> \n" +
                "</Message>  \n" ;


        List<Product> skuList= new ArrayList<>();

        for(Product product :productList){
            String[] skus =  product.getProps().split(";");
            for(int i = 0;i<skus.length;i++){
                Product skuProduct = new Product();
                skuProduct.setSKU(shop.getId()+ "-"+product.getSKU()+"-"+ String.valueOf(i));
                skuProduct.setConditions(product.getConditions());
                skuProduct.setTitle(product.getTitle());
                skuProduct.setDescription(product.getDescription());
                skuProduct.setProductClass(product.getProductClass());
                skuProduct.setBulletPoint(product.getBulletPoint());

                skuList.add(skuProduct);
            }
        }
        productList.addAll(skuList);


        String html ="";
        for(int i=0;i<productList.size();i++){
            Product product = productList.get(i);

            String xml=inventoryItem.replace("{SKU}",shop.getId()+ "-"+product.getSKU());
            xml=xml.replace("{index}",String.valueOf(i+1));
            //xml=xml.replace("{sid}", shop.getShopcode());

            html+=xml;
        }

        String xmlInventory = PRODUCT_INVENTORY_TEMPLATE.replace("{sid}",shop.getShopcode());

        xmlInventory=xmlInventory.replace("{inventory}", html);

        return xmlInventory;
    }

    public static String toPriceXML(List<Product> productList,Shop shop){




        String priceItem="<Message>\n" +
                "<MessageID>{index}</MessageID>\n" +
                "<Price>\n" +
                "<SKU>{SKU}</SKU>\n" +
                "<StandardPrice currency=\"{GBP}\">{price}</StandardPrice>\n" +
                "<Sale>\n" +
                "<StartDate>2018-10-01T00:00:00Z</StartDate>\n" +
                "<EndDate>2019-01-31T00:00:00Z</EndDate>\n" +
                "<SalePrice currency=\"{GBP}\">{price1}</SalePrice>\n" +
                "</Sale> \n"+
                "</Price>\n" +
                "</Message>\n" ;

        List<Product> skuList= new ArrayList<>();

        for(Product product :productList){
            String[] skus =  product.getProps().split(";");
            for(int i = 0;i<skus.length;i++){
                Product skuProduct = new Product();
                skuProduct.setSKU(shop.getId()+ "-"+product.getSKU()+"-"+ String.valueOf(i));
                skuProduct.setConditions(product.getConditions());
                skuProduct.setTitle(product.getTitle());
                skuProduct.setDescription(product.getDescription());
                skuProduct.setProductClass(product.getProductClass());
                skuProduct.setBulletPoint(product.getBulletPoint());
                skuProduct.setMSRP(product.getMSRP());

                skuList.add(skuProduct);
            }
        }
        productList.addAll(skuList);

        String html ="";
        for(int i=0;i<productList.size();i++){
            Product product = productList.get(i);

            String xml=priceItem.replace("{SKU}",shop.getId()+ "-"+product.getSKU());
            xml=xml.replace("{index}",String.valueOf(i+1));

            DecimalFormat df = new DecimalFormat("######0.00");
            Double price = Double.parseDouble(product.getMSRP())*2/8.8;
            xml=xml.replace("{price}",df.format(price));
            xml=xml.replace("{price1}",df.format(price-3));

            if(shop.getVisitcode().equals("A1F83G8C2ARO7P")){

                xml=xml.replace("{GBP}","DEFAULT");
            }else{
                xml=xml.replace("{GBP}","DEFAULT");
            }


            html+=xml;
        }

        String xmlPrice = PRODUCT_PRICE_TEMPLATE.replace("{sid}",shop.getShopcode());

        xmlPrice=xmlPrice.replace("{prices}", html);

        return xmlPrice;
    }

    public static String toImageXML(List<Product> productList,Shop shop){



        String temp = "<Message>\n" +
                "<MessageID>{index}</MessageID>\n" +
                "<OperationType>Update</OperationType>\n" +
                "<ProductImage>\n" +
                "<SKU>{SKU}</SKU>\n" +
                "<ImageType>{type}</ImageType>\n" +
                "<ImageLocation>{img}</ImageLocation>\n" +
                "</ProductImage>\n" +
                "</Message>\n" ;


        List<Product> skuList= new ArrayList<>();

        for(Product product :productList){
            if(product.getProps()==null){
                continue;
            }
            String[] skus =  product.getProps().split(";");

            for(int i = 0;i<skus.length;i++){

                if("".equals(skus[i])){
                    continue;
                }

                Product skuProduct = new Product();
                skuProduct.setSKU(shop.getId()+ "-"+product.getSKU()+"-"+ String.valueOf(i));

                String url =skus[i].split(",")[1].replace("imageUrl:","");

                skuProduct.setBigImg(url+",");


                skuList.add(skuProduct);

                product.setBigImg(product.getBigImg().replace(url,""));
            }
        }
        productList.addAll(skuList);




        int count =1;

        String htmms = "";
        for(int j=0;j<productList.size();j++){

            Product product = productList.get(j);
            String imgs = product.getBigImg();

            imgs=imgs.substring(0,imgs.length()-1);

            String[] urls = imgs.split(",");

            for(int i=0;i<urls.length;i++){

                if(urls[i].equals("")){
                    continue;
                }

                String html  = temp;

                html=html.replace("{index}",String.valueOf(count));
                html=html.replace("{SKU}",shop.getId()+ "-"+product.getSKU());
                html=html.replace("{img}",urls[i]);
                if(i==0){
                    html=html.replace("{type}","Main");
                }else{
                    html=html.replace("{type}","PT"+i);
                }

                count=count+1;

                htmms+=html;

            }
        }

        String xml= PRODUCT_IMAGE_TEMPLATE.replace("{imgs}",htmms);

        xml=xml.replace("{sid}", shop.getShopcode());

        return xml;
    }

    public static String toVariationXML(List<Product> productList, Shop shop) {







        String temp=
                "<Message>\n" +
                    "<MessageID>{index}</MessageID>\n" +
                    "<OperationType>Update</OperationType>\n" +
                    "<Relationship>\n" +
                        "<ParentSKU>{psku}</ParentSKU>\n" +
                        "{var}"+
                        "</Relationship>\n" +
                "</Message>\n";
        String temp1 ="<Relation>\n" +
                            "<SKU>{sku}</SKU>\n" +
                            "<Type>Variation</Type>\n" +
                        "</Relation>\n";

        int count =1;

        String htmms = "";
        for(int j=0;j<productList.size();j++){

            Product product = productList.get(j);
            String props = product.getProps();

            if("".equals(props)){
                continue;
            }

            String[] urls = props.split(";");


            String ar=temp;
            String items="";

            String psku= shop.getId()+ "-"+product.getSKU();


            ar=ar.replace("{psku}",psku);
            ar=ar.replace("{index}",String.valueOf(count));



            count=count+1;

            for(int i=0;i<urls.length;i++){

                if(urls[i].equals("")){
                    continue;
                }

                String html  = temp1;

                html=html.replace("{sku}",shop.getId()+ "-"+product.getSKU()+'-'+i);

                items+=html;
            }

            ar=ar.replace("{var}",items);

            htmms+=ar;
        }


        String xml= PRODUCT_VAR_TEMPLATE.replace("{vars}",htmms);

        xml=xml.replace("{sid}", shop.getShopcode());

        return xml;
    }
}
